package com.boot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.boot.entity.User;
import com.boot.mapper.RoleMenuMapper;
import com.boot.pojo.Result;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boot.service.IRoleService;
import com.boot.entity.Role;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shuchong
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    //新增或者更新
    @PostMapping("/saveOrUpdate")
    public Result save(@RequestBody Role role){
        boolean flag;
        if(Objects.isNull(role.getId())){
            flag = roleService.saveOrUpdate(role);
            if(flag){
                return new Result().success("保存成功");
            }else{
                return new Result().fail("保存失败");
            }
        }else{
            flag = roleService.saveOrUpdate(role);
            if(flag){
                return new Result().success("修改成功");
            }else{
                return new Result().fail("修改失败");
            }
        }
    }
    //删除单个
    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable Integer id){
        boolean flag = roleService.removeById(id);
        if(flag){
            return new Result().success("执行成功");
        }else{
            return new Result().fail("执行失败");
        }
    }
    //批量删除
    @PostMapping("/dels")
    public Result delete(@RequestBody List<Integer> ids){
        boolean flag = roleService.removeByIds(ids);
        if(flag){
            return new Result().success("执行成功");
        }else{
            return new Result().fail("执行失败");
        }
    }
    //查找全部
    @GetMapping("/findAll")
    public Result findAll(){
        List<Role> list = roleService.list();
        return new Result().add("list",list);
    }
    //获取数据
    @GetMapping("/getData/{name}")
    public Result getData(@PathVariable("name")String name){
        QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        Role role = roleService.getOne(queryWrapper);
        return new Result().success("数据获取成功").add("role",role);
    }

    //分页
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "5") Integer pageSize,
                           @RequestParam(defaultValue = "") String name){
        IPage<Role> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if(!"".equals(name)){
            queryWrapper.like("name",name);
        }
        IPage<Role> page1 = roleService.page(page,queryWrapper);
        return new Result().add("page",page1);
    }

    @PostMapping("/roleMenu/{roleId}")
    public Result roleMenu(@PathVariable("roleId") Integer roleId,@RequestBody List<Integer> menuIds){
        roleService.setRoleMenu(roleId,menuIds);
        return new Result().success("执行成功");
    }
    @GetMapping("/roleMenuId/{roleId}")
    public Result getRoleMenu(@PathVariable Integer roleId){
        return new Result().success().add("list",roleService.getRoleMenu(roleId));
    }
}
